package acme.ws;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import acme.client.API;
import acme.client.ClientManager;

public class ClientManagerServlet extends JSONServlet{

	private static final long serialVersionUID = 8893392323182942094L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = req.getPathInfo();
		
		if(path == null || path.indexOf('/') == -1 || path.length() == 1){
			returnJSON(ClientManager.getWorkerMap(), resp);
		}else{
			StringTokenizer tokenizer = new StringTokenizer(path, "/");
			String name = tokenizer.nextToken();
			API api = API.valueOf(name);
			if(api != null){
				if(tokenizer.hasMoreElements()){
					String s = tokenizer.nextToken();
					if(s != null){
						int num = Integer.parseInt(s);
						ClientManager.runWorkers(api, num);
					}
				}
			}
			
			returnJSON(ClientManager.getWorkerMap(), resp);
		}
	}
	
	
}
